package com.shizhanzhe.zxbapp.vip;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.PDDSearch;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.LineEditText;
import com.shizhanzhe.zxbapp.widget.SortView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zz9527 on 2018/12/19.
 */
public class SearchActivity extends AppCompatActivity implements SortView.notifyOutsideListener {

    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.et_coin)
    LineEditText etCoin;
    @BindView(R.id.go)
    ImageView go;
    @BindView(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;
    @BindView(R.id.sortView)
    SortView sortView;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    private String[] mVale = {"食品", "服装", "箱包", "日用"};
    private List<String> sortType = Arrays.asList("综合", "价格", "佣金比例");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_goods);
        ImmersionBar.with(this).statusBarColor(R.color.them).fitsSystemWindows(true)
                .init();
        ButterKnife.bind(this);
        final LayoutInflater mInflater = LayoutInflater.from(this);
        idFlowlayout.setAdapter(new TagAdapter<String>(mVale) {
            @Override
            public View getView(FlowLayout parent, int position, String o) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tv2, parent, false);
                tv.setText(o);
                return tv;
            }
        });
        idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                etCoin.setText(mVale[position]);
                getData();
                ll.setVisibility(View.VISIBLE);
                ll1.setVisibility(View.GONE);
                return true;
            }
        });
        Drawable[] sortDrawable = {ContextCompat.getDrawable(this, R.mipmap.icon_sort_defalut), ContextCompat.getDrawable(this, R.mipmap.icon_sort_true), ContextCompat.getDrawable(this, R.mipmap.icon_sort_false)};
        sortView.setType(sortType, sortDrawable);
        sortView.setNotifyOutsideListener(this);
        etCoin.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if(i == EditorInfo.IME_ACTION_SEARCH) {
                    String key = etCoin.getText().toString().trim();
                    if (TextUtils.isEmpty(key)) {
                        Toast.makeText(SearchActivity.this, "搜索不能为空", Toast.LENGTH_SHORT).show();
                        return true;
                    }
                    getData();
                    ll.setVisibility(View.VISIBLE);
                    ll1.setVisibility(View.GONE);
                }
                return false;
            }
        });
    }


    @OnClick({R.id.iv_close, R.id.go})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finish();
                break;
            case R.id.go:
                if (!TextUtils.isEmpty(etCoin.getText().toString())) {
                    getData();
                    ll.setVisibility(View.VISIBLE);
                    ll1.setVisibility(View.GONE);
                } else {
                    ll.setVisibility(View.GONE);
                    ll1.setVisibility(View.VISIBLE);
                    Toast.makeText(this, "搜索不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    int type = 1;
    private void getData() {
        Log.i("________",etCoin.getText().toString());
        OkHttpDownloadJsonUtil.downloadJson(this, Path.PDDSEARCH(etCoin.getText().toString(), type), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                try {
                    Gson gson = new Gson();
                    final List<PDDSearch.DataBean> data = gson.fromJson(json, PDDSearch.class).getData();
                    if (data.size() == 0) {
                        Toast.makeText(SearchActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    SearchAdapter adapter=new SearchAdapter(data, SearchActivity.this);
                    lv.setAdapter(adapter);
                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            startActivity(new Intent(SearchActivity.this, PDDDetailActivity.class).putExtra("goods_id", data.get(i).getGoods_id()+""));
                        }
                    });
                } catch (Exception e) {
                    Log.i("______--",e.toString());
                    Toast.makeText(SearchActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    @Override
    public void notifySort(String tag, int desOrasc) {
        if (desOrasc == 0) {//由低到高
            if (tag.equals("价格")) {
                type = 2;
                getData();
            } else if (tag.equals("佣金比例")) {
                type = 4;
                getData();
            }
        } else {//由高到低
            if (tag.equals("综合")) {
                type = 1;
                getData();
            } else {
                if (tag.equals("价格")) {
                    type = 3;
                    getData();
                } else if (tag.equals("佣金比例")) {
                    type = 5;
                    getData();
                }
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
